keepalived-nginx(主主高可用)
=======================================
一：设计要点
    1：一个地址绑定dns多个A记录，轮询到每一台主机。
    2：keepalived每隔一段时间通过一个脚本检测nginx的可用性。
    3：每台代理服务器必须是高可用，不能出现dns解析的地址找不到ip的情况。
    4：服务器之间时间同步。
    6：检测脚本
            vrrp_script chk_down {
                script "脚本"
                # 多久检测一次 秒
                interval 2
                # 权重增加还是减小
                weight -5
            }
        注释：
            (1)： 优先级不会不断的提高或者降低
            (2)：可以编写多个检测脚本并为每个检测脚本设置不同的weight
            (3)：优先级范围是 0-255
            (4)：如果脚本执行结果为0，并且weight配置的值大于0，则优先级相应的增加
                    如果脚本执行结果非0，并且weight配置的值小于0，则优先级相应的减少
    7：安装程序，此处是CentOS7最小化安装，有些包不存在。
        ~]# yum -y install mailx psmisc.x86_64 keepalived nginx net-tools.x86_64
        注释：mail 包是 mailx。killall 包是 psmisc.x86_64，ifconfig 包是 net-tools.x86_64
    8：主主模型是两台机子互为主备。
    9：vrrp_mcast_group4：IPV4多播地址，默认224.0.0.18。

<img src="6.png" alt="">

二：时间同步，写计划任务
    时间服务器：172.18.0.1
    ~]# which ntpdate
    /usr/sbin/ntpdate
    ~]# crontab -e
    */5 * * * *  /usr/sbin/ntpdate 172.18.0.1 > /dev/null


三：实现
    1：代理服务器：172.18.21.70  172.18.21.71 安装 keepalived 与 nginx 服务。
        ~]# yum -y install keepalived nginx

    2：keepalived配置

        2.1：172.18.21.70
            ~]# vi /etc/keepalived/keepalived.conf

-----shell  start-------
                global_defs {
                   notification_email {
                     root@localhost
                   }
                   notification_email_from mdx@magedu.com
                   smtp_server 127.0.0.1
                   smtp_connect_timeout 30
                   router_id node1
                   vrrp_mcast_group4 224.0.21.18
                }
                vrrp_script chk_down {
                    script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
                    interval 2
                    weight -5
                }
                vrrp_script chk_nginx {
                    script "killall -0 nginx"
                    interval 2
                    weight -5
                }
                vrrp_instance VI_1 {
                    state MASTER
                    interface eno16777736
                    virtual_router_id 51
                    priority 100
                    advert_int 1
                    authentication {
                        auth_type PASS
                        auth_pass 11111111
                    }
                    virtual_ipaddress {
                        172.18.21.68 dev eno16777736 label eno16777736:0
                    }
                    track_script {
                        chk_down
                        chk_nginx
                    }
                    notify_master "/etc/keepalived/notify.sh master"
                    notify_backup "/etc/keepalived/notify.sh backup"
                    notify_fault "/etc/keepalived/notify.sh fault"
                }
                vrrp_instance VI_2 {
                    state BACKUP
                    interface eno16777736
                    virtual_router_id 52
                    priority 98
                    advert_int 1
                    authentication {
                        auth_type PASS
                        auth_pass 22222222
                    }
                    virtual_ipaddress {
                        172.18.21.69 dev eno16777736 label eno16777736:1
                    }
                    track_script {
                        chk_down
                        chk_nginx
                    }
                    notify_master "/etc/keepalived/notify.sh master"
                    notify_backup "/etc/keepalived/notify.sh backup"
                    notify_fault "/etc/keepalived/notify.sh fault"
                }
-----shell  end-------


        2.2：192.168.21.71
            ~]# vi /etc/keepalived/keepalived.conf

-----shell  start-------
                global_defs {
                    notification_email {
                        root@localhost
                    }
                    notification_email_from mdx@magedu.com
                    smtp_server 127.0.0.1
                    smtp_connect_timeout 30
                    router_id node1
                    vrrp_mcast_group4 224.0.21.18
                }
                vrrp_script chk_down {
                    script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
                    interval 2
                    weight -5
                }
                vrrp_script chk_nginx {
                    script "killall -0 nginx"
                    interval 2
                    weight -5
                }
                vrrp_instance VI_1 {
                    state BACKUP
                    interface eno16777736
                    virtual_router_id 51
                    priority 98
                    advert_int 1
                    authentication {
                        auth_type PASS
                        auth_pass 11111111
                    }
                    virtual_ipaddress {
                        172.18.21.68 dev eno16777736 label eno16777736:0
                    }
                    track_script {
                        chk_down
                        chk_nginx
                    }
                    notify_master "/etc/keepalived/notify.sh master"
                    notify_backup "/etc/keepalived/notify.sh backup"
                    notify_fault "/etc/keepalived/notify.sh fault"
                }
                vrrp_instance VI_2 {
                    state MASTER
                    interface eno16777736
                    virtual_router_id 52
                    priority 100
                    advert_int 1
                    authentication {
                        auth_type PASS
                        auth_pass 22222222
                    }
                    virtual_ipaddress {
                        172.18.21.69 dev eno16777736 label eno16777736:1
                    }
                    track_script {
                        chk_down
                        chk_nginx
                    }
                    notify_master "/etc/keepalived/notify.sh master"
                    notify_backup "/etc/keepalived/notify.sh backup"
                    notify_fault "/etc/keepalived/notify.sh fault"
                }
-----shell  end-------


        2.3：通知脚本（172.18.21.70与172.18.21.71）
            ~]# vi /etc/keepalived/notify.sh
            ~]# chmod +x /etc/keepalived/notify.sh

-----shell  start-------
            #!/bin/bash
            #
            contact='root@localhost'
            notify() {
                mailsubject="$(hostname) to be $1: vip floating"
                mailbody="$(date +'%F %T'): vrrp transition, $(hostname) change to be $1"
                echo $mailbody | mail -s "$mailsubject" $contact
            }
            case $1 in
            master)
                notify master
                systemctl  start  nginx.service
                ;;
            backup)
                notify backup
                systemctl  restart nginx.service
                ;;
            fault)
                notify fault
                ;;
            *)
                echo "Usage: $(basename $0) {master|backup|fault}"
                ;;
            esac
-----shell  end-------

        2.4：配置页
            172.18.21.70 和 172.18.21.71 写入不同的页面内容，以便于观测效果。
            ~]# vi /usr/share/nginx/html/index.html

四：测试
    1：关闭其中任意一台的服务。
        ~]# systemctl stop nginx.service
        访问：
        ~]# curl 172.18.21.68
        ~]# curl 172.18.21.69

    2：写入停机文件 down
        ~]# touch /etc/keepalived/down
        访问：
        ~]# curl 172.18.21.68
        ~]# curl 172.18.21.69